package com.programmingteam.wallpaperslideshow.service;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.programmingteam.wallpaperslideshow.Config;

public class AlarmReceiver extends BroadcastReceiver {
	//private static final String TAG = "WallpaperSlideshow";
	public static final int REQUEST_CODE = 1024;

	@Override
	public void onReceive(Context context, Intent intent) {
		Calendar cal;
		PendingIntent sender;
		AlarmManager alarmManager;
		boolean isScheduled, isConectivityEnabled, isPhotoData, isNextPhoto, isLocation;
		long imageId;
		
		// Read intent parameters
		isConectivityEnabled = isConnectivityAvailable(context);
		isScheduled = intent.getBooleanExtra(Config.Const.F_SCHEDULED, false);
		alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		
		// Check connectivity
		if (!isConectivityEnabled) {
			if (isScheduled) {
				cal = Calendar.getInstance();
				cal.add(Calendar.MINUTE, Config.Service.NO_CONNECTION_TIMER_INTERVAL);
				sender = PendingIntent.getBroadcast(context, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
				alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
			}
			else {
				Toast.makeText(context, "Intenet connection disabled!", Toast.LENGTH_SHORT).show();
			}
			return;
		}
		
		// Rewrite intent extras
		Intent serviceIntent = new Intent(context, WallpaperService.class);
		isPhotoData = intent.getBooleanExtra(Config.Const.F_PHOTODATA, false);
		isLocation = intent.getBooleanExtra(Config.Const.F_LOCATION, false);
		isNextPhoto = intent.getBooleanExtra(Config.Const.F_NEXTPHOTO, true);
		imageId = intent.getLongExtra(Config.Const.D_PHOTODATA_ID, 0);
		serviceIntent.putExtra(Config.Const.F_LOCATION, isLocation);
		serviceIntent.putExtra(Config.Const.F_NEXTPHOTO, isNextPhoto);
		serviceIntent.putExtra(Config.Const.F_PHOTODATA, isPhotoData);
		serviceIntent.putExtra(Config.Const.D_PHOTODATA_ID, imageId);
		context.startService(serviceIntent);
		
		// Schedule next broadcast
		if(isScheduled) {
			cal = Calendar.getInstance();
			//cal.add(Calendar.MINUTE, getConfigTimeInterval(context));
			cal.add(Calendar.SECOND, 5);
			sender = PendingIntent.getBroadcast(context, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
		}
	}

	public boolean isConnectivityAvailable(Context c) {
		SharedPreferences config;
		ConnectivityManager cm;
		boolean WiFiOnly, typeWiFi, typeMobile;

		config = PreferenceManager.getDefaultSharedPreferences(c);
		cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
		WiFiOnly = config.getBoolean(Config.SharedKeys.WIFI_ONLY, Config.SharedDefaults.WIFI_ONLY);
		typeWiFi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
		typeMobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();

		return (WiFiOnly && typeWiFi) || (!WiFiOnly && (typeWiFi || typeMobile));
	}

	public int getConfigTimeInterval(Context c) {
		SharedPreferences config;
		int nextWallpaper;

		config = PreferenceManager.getDefaultSharedPreferences(c);
		nextWallpaper = config.getInt(Config.SharedKeys.EVENT_TIMER_INTERVAL, Config.SharedDefaults.EVENT_TIMER_INTERVAL);

		return nextWallpaper;
	}
}
